<template>
  <view class="content">
    <button @click="send()"> 发送消息 </button>
    <view class="item" v-for="it in list" :key="it">{{ it }}</view>
  </view>
</template>

<script setup>
  import { ref } from 'vue'
  const list = ref([])
  let socketTask = uni.connectSocket({
    url: 'ws://127.0.0.1:8080/websocket/common_001',
    success(res) {
      console.log('创建连接', res)
    }
  })
  socketTask.onOpen(res => {
    console.log('onOpen')
  })
  socketTask.onError(err => {
    console.log('onError', err)
  })
  socketTask.onClose(res => {
    console.log('onClose')
  })
  socketTask.onMessage(res => {
    list.value.push(res.data)
    console.log('onMessage', res)
  })

  function send() {
    socketTask.send({ data: '{"msg":"abcdefg"}' })
  }
</script>

<style>
</style>